package edu.tamu.cse.teamproject1.Database;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import edu.tamu.cse.teamproject1.Database.Interfaces.ITuple;
import edu.tamu.cse.teamproject1.Database.Interfaces.ITupleElement;

/**
 * Class Tuple allows the addition of tuples to a table
 * it consists of tuple elements that have a type and a value
 *
 *@author Ben Unsworth
 *@since Release 0.1.0
 */
public class Tuple implements ITuple {
	/**
	 * The unique ID of the tuple
	 */
	private long uniqueId;
	
	List<ITupleElement> tupleElements;
	
	/**
	 * Constructor, creates an empty tuple
	 */
	public Tuple(){
		tupleElements = new ArrayList<ITupleElement>();
		Random randomGenerator = new Random();
		randomGenerator.setSeed(937167214698213L);
		uniqueId = randomGenerator.nextLong();
	}
	
	/**
	 * Creates a tuple with TupleElements
	 * @param tuple
	 */
	public Tuple(List<ITupleElement> tuple){
		tupleElements = tuple;
		Random randomGenerator = new Random();
		randomGenerator.setSeed(937167214698213L);
		uniqueId = randomGenerator.nextLong();
	}
	
	/**
	 * Delete's a tuple element
	 * @param tupleElementIndex
	 */
	void deleteTupleElement(int tupleElementIndex){
		if(tupleElements.isEmpty() || tupleElements.size() < tupleElementIndex) return;
		else tupleElements.remove(tupleElementIndex);
	}

	
	/**
	 * Gets all of the elements in a tuple
	 * @return the list of tupleElements for this tuple
	 */
	@Override
	public List<ITupleElement> getAllElements() {
		return tupleElements;
	}

	/**
	 * Adds a tuple element to the list of elements on this object
	 * 
	 * @param value Value of the tuple element
	 * @author Christopher Weldon <chris@chrisweldon.net>
	 */
	@Override
	public void addTupleElement(ITupleElement value) {
		tupleElements.add(value);
	}

	/**
	 * Adds a tuple element to the list of elements in this object
	 * 
	 * @param value Value of the tuple element
	 * @param size Maximum length of this element
	 * @author Christopher Weldon <chris@chrisweldon.net>
	 */
	@Override
	public void addTupleElement(Object value, int size) {
		if (value instanceof String) {
			tupleElements.add(new TupleVarCharElement((String)value, size));
		} else {
			throw new IllegalArgumentException("Invalid element type.");
		}
	}
	
	/**
	 * Deletes a tuple element from the list of elements
	 * 
	 * @param Object element Element to delete
	 */
	@Override
	public void deleteTupleElement(Object element) {
		this.tupleElements.remove(element);
	}
	
	/**
	 * Returns the unique ID of the tuple
	 * 
	 * @return the unique ID of the tuple
	 * @author Christopher Weldon <chris@chrisweldon.net>
	 */
	@Override
	public long getUniqueId() {
		return this.uniqueId;
	}

	/**
	 * Implements deep copying objects
	 * 
	 * @return Copy of this object
	 */
	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}
}
